/**
 * 
 */
package util;

/**
 * 金额数据转换为中文大写金额
 * @author LiuZhuDong
 *
 */
public class TestMoneyConvert {
	/**
	 * 整数部分的单位集
	 */
	private static String[] unitStrs = { "", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万" };
	/**
	 * 十进制对应的中文大写集
	 */
	private static String[] numberStrs = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	/**
	 * 小数部分的单位集
	 */
	private static String[] floatUnitStrs = { "", "角", "分" };

	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		String testNumber = "qwrqw";
		System.out.println(convertChinese(testNumber));
	}

	/**
	 * 金额数据转换为中文大写金额
	 * @param moneyStr
	 * @return
	 */
	public static String convertChinese(String moneyStr) {
		String result = "";
		if (moneyStr != null && !"".equals(moneyStr)) {
			int pointIndex = moneyStr.indexOf(".");
			String intNumberStr = ""; // 整数部分
			String floatNumberStr = "";// 小数部分
			
			/* 通过小数点来判断是否有小数 */
			if (pointIndex > 0) {
				intNumberStr = new String(moneyStr.substring(0, pointIndex));
				floatNumberStr = new String(moneyStr.substring(pointIndex + 1, moneyStr.length()));
			}
			/* 整数部分转换 */
			if (!"".equals(intNumberStr)) {
				long intNumber = Long.parseLong(intNumberStr);
				if (intNumber == 0L) {
					result += (numberStrs[0] + unitStrs[1]);
				} else {
					result +=convertNumber(intNumberStr, true);
				}
			}
			/* 小数数部分转换 */
			if (!"".equals(floatNumberStr)) {
				int floatNumber = Integer.parseInt(floatNumberStr);
				if (floatNumber == 0) {
					result +="整";
				} else {
					result +=convertNumber(floatNumberStr, false);
				}
			}
		}
		return result;
	}
	
	/**
	 * 具体实现：金额数据转换为中文大写金额
	 * @param numberStr
	 * @param isIntFlag
	 * @return
	 */
	private static String convertNumber(String numberStr,boolean isIntFlag){
		String result = "";
		char[] chars = numberStr.toCharArray();
		char baseC = '0';
		int i = isIntFlag ? chars.length :1;
		StringBuilder strb = new StringBuilder(100);
		for (char c : chars) {
			int index = (c - baseC);
			if(isIntFlag){
				strb.append(getConvertIntStr(index, i--));
			}else{
				strb.append(numberStrs[index] + floatUnitStrs[i++]);
			}
			
		}
		result =strb.toString();
		return result;
	}

	/**
	 * 获取转换之后的整数部分的字符串
	 * @param index
	 * @param i
	 * @return
	 */
	private static String getConvertIntStr(int index, int i) {
		String str = "";
		if (index == 0) {
			if (i == 1) {
				str = unitStrs[i];
			}
		} else {
			str = numberStrs[index] + unitStrs[i];
		}
		return str;
	}

}

